package com.ylkj.patient.utils;

import android.content.Context;

import com.examination.mlib.constants.ProjectConfigure;
import com.examination.mlib.constants.UserInfoConstants;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.api.UPushRegisterCallback;
import com.umeng.message.entity.UMessage;
import com.yilijk.base.network.https.NetAttestationUtils;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.SharedPreferencesUtils;
import com.yljk.live.common.utils.MediaCountyManager;

import javax.annotation.Nullable;

public class UmengUtils {

    private static boolean initialized = false;

    /**
     * 预初始化
     */
    public static void preInit(Context context) {
        //解决厂商通知点击时乱码等问题
        PushAgent.setup(context,
                DevicesUtils.isEnvironment() ? ProjectConfigure.Umeng_AppKey_Testing : ProjectConfigure.Umeng_AppKey_onLine,
                DevicesUtils.isEnvironment() ? ProjectConfigure.Umeng_Message_Secret_Testing : ProjectConfigure.Umeng_Message_Secret_onLine
        );
        UMConfigure.preInit(context,
                DevicesUtils.isEnvironment() ? ProjectConfigure.Umeng_AppKey_Testing : ProjectConfigure.Umeng_AppKey_onLine,
                "Umeng"
        );
    }


    //友盟
    public static void initUmeng(Context context, @Nullable UmengListener listener) {

        // 参数一：当前上下文context；
        // 参数二：应用申请的Appkey（需替换）；
        // 参数三：渠道名称；
        // 参数四：设备类型，必须参数，传参数为UMConfigure.DEVICE_TYPE_PHONE则表示手机；传参数为UMConfigure.DEVICE_TYPE_BOX则表示盒子；默认为手机；
        // 参数五：Push推送业务的secret 填充Umeng Message Secret对应信息（需替换）
        /**
         * 测试data
         * appKey:5ffd4257784e8c1af2d38037
         * Umeng Message Secret:8bf400d2d9cdef6ce5d60d4e135d3319
         * 正式
         * AppKey：5f2ba64060a5a254af179b70
         * Umeng Message Secret：42c79ec39a6453d286c3344ec54b0c53
         * App Master Secret：u3j6mnkazohpnw4rrqttsma1fpsa5k0w
         */
        UMConfigure.init(context,
                DevicesUtils.isEnvironment() ? ProjectConfigure.Umeng_AppKey_Testing : ProjectConfigure.Umeng_AppKey_onLine,
                "Umeng",
                UMConfigure.DEVICE_TYPE_PHONE,
                DevicesUtils.isEnvironment() ? ProjectConfigure.Umeng_Message_Secret_Testing : ProjectConfigure.Umeng_Message_Secret_onLine);
        //获取消息推送代理示例
        PushAgent mPushAgent = PushAgent.getInstance(context);
        mPushAgent.setResourcePackageName("com.ylkj.patient");

//        mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SERVER); //服务端控制声音
        //注册推送服务，每次调用register方法都会回调该接口
//        mPushAgent.register(new IUmengRegisterCallback() {
        mPushAgent.register(new UPushRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                initialized = true;
                //注册成功会返回deviceToken deviceToken是推送消息的唯一标志
                ALog.i("getUmengToken", "注册成功：deviceToken：-------->  " + deviceToken);
                SharedPreferencesUtils.getInstance().saveUment("deviceToken", deviceToken);
                if (listener != null) {
                    listener.onTokenSuccess();
                }
            }

            @Override
            public void onFailure(String s, String s1) {
                ALog.e("getUmengToken", "注册失败：-------->  " + "s:" + s + ",s1:" + s1);
                if (listener != null) {
                    listener.onTokenFailure();
                }
            }
        });
        //数据统计
//        PushAgent.getInstance(context).onAppStart();

        UmengNotificationClickHandler umengNotificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage uMessage) {
                super.dealWithCustomAction(context, uMessage);
                String uriString = uMessage.extra.get("scheme");
                boolean isJumpLive = MediaCountyManager.getInstance().pageClick(uriString);
                ALog.e("oakkk", "isJumpLive: " + isJumpLive);
                if (!isJumpLive) {
                    PushMessageUtil.getInstance().dealMessage(context, uriString);
                }
            }
        };


    }

    public static boolean isInitialized() {
        return initialized;
    }

    public static void setInitialized(boolean initialized) {
        UmengUtils.initialized = initialized;
    }


    public interface UmengListener {
        void onTokenSuccess();

        void onTokenFailure();
    }

    /**
     * 添加友盟别名
     *
     * @param context
     */
    public static void addUmengAddAlias(Context context) {
        PushAgent mPushAgent = PushAgent.getInstance(context);
        String passportId = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.account_passportId, "");
        ALog.e("getUmengToken", "passportId: " + passportId);
        mPushAgent.setAlias(passportId, "kUMAliasH1txTypeSina", new UTrack.ICallBack() {
            @Override
            public void onMessage(boolean b, String s) {
                ALog.e("getUmengToken", "onMessage set: " + b + " ==> " + s);
            }
        });
        mPushAgent.addAlias(passportId, "kUMAliasH1txTypeSina", new UTrack.ICallBack() {
            @Override
            public void onMessage(boolean b, String s) {
                ALog.e("getUmengToken", "onMessage add: " + b + " ==> " + s);
            }
        });
    }

}
